package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.behavior.entity.ApFollowBehavior;
import com.usian.behavior.mapper.ApFollowBehaviorMapper;
import com.usian.behavior.service.IApFollowBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP关注行为表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-07-18
 */
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements IApFollowBehaviorService {
    /**
     * 存储关注数据
     * @param dto
     */
    @Override
    public void saveFollowBehavior(FollowBehaviorDto dto) {
        //保证关注行为 基于文章关注
        //根据文章查询这个用户是否已经关注这个文章
        LambdaQueryWrapper<ApFollowBehavior> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApFollowBehavior::getApUserId,dto.getUserId()).eq(ApFollowBehavior::getArticleId,dto.getArticleId());

        ApFollowBehavior one = getOne(lambdaQueryWrapper);
        if (one!=null){
            throw new LeadException(AppHttpCodeEnum.DATA_EXIST,"该用户已经关注过该文章");
        }
        ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
        apFollowBehavior.setApUserId(dto.getUserId());
        apFollowBehavior.setCreatedTime(new Date());
        apFollowBehavior.setArticleId(dto.getArticleId());
        apFollowBehavior.setFollowId(dto.getFollowId());
        save(apFollowBehavior);
    }
}
